Migration Guide topic
Migration Guide
This document gathered all breaking changes and migrations requirement between versions.
Breaking versions
5.0.0
Summary
get
andgetUri
inDio
has different signature.DefaultHttpClientAdapter
is now namedIOHttpClientAdapter
, and the platform independent adapter can be initiated byHttpClientAdapter()
which is a factory method.- Adapters that extends
HttpClientAdapter
must nowimplements
instead ofextends
. DioError
has separate constructors and all fields are annotated as final.DioErrorType
has different values.- Imports are split into new libraries:
dio/io.dart
is for natives specific classes;dio/browser.dart
is for web specific classes.
connectTimeout
,sendTimeout
, andreceiveTimeout
are nowDuration
instead ofint
.
Details
get
and getUri
Future<Response<T>> get<T>(
String path, {
+ Object? data,
Map<String, dynamic>? queryParameters,
Options? options,
CancelToken? cancelToken,
ProgressCallback? onReceiveProgress,
});
Future<Response<T>> getUri<T>(
Uri uri, {
+ Object? data,
Map<String, dynamic>? queryParameters,
Options? options,
CancelToken? cancelToken,
ProgressCallback? onReceiveProgress,
});
HttpClientAdapter
Before:
void initAdapter() {
final dio = Dio();
// For natives.
dio.httpClientAdapter = DefaultHttpClientAdapter();
// For web.
dio.httpClientAdapter = BrowserHttpClientAdapter();
}
After:
void initAdapter() {
final dio = Dio();
// Universal adapter that create the adapter for the corresponding platform.
dio.httpClientAdapter = HttpClientAdapter();
// For natives.
dio.httpClientAdapter = IOHttpClientAdapter();
// For web.
dio.httpClientAdapter = BrowserHttpClientAdapter();
}
Implementing HttpClientAdapter
Before:
class ExampleAdapter extends HttpClientAdapter { /* ... */ }
After:
class ExampleAdapter implements HttpClientAdapter { /* ... */ }
Const DioError
Before:
Never throwDioError() {
final error = DioError(request: requestOptions, error: e);
error.message = 'Custom message.';
error.stackTrace = StackTrace.current;
throw error;
}
After:
Never throwDioError() {
DioError error = DioError(
request: requestOptions,
error: e,
stackTrace: StackTrace.current
);
error = error.copyWith(message: 'Custom message.');
throw error;
}
DioErrorType
values update
Before | After |
---|---|
N/A | badCertificate |
response | badResponse |
connectTimeout | connectionTimeout |
other | unknown |
Duration
instead of int
for timeouts
Before:
void request() {
final dio = Dio(
BaseOptions(
connectTimeout: 5000,
sendTimeout: 5000,
receiveTimeout: 10000,
),
);
}
After:
void request() {
final dio = Dio(
BaseOptions(
connectTimeout: const Duration(seconds: 5),
sendTimeout: const Duration(seconds: 5),
receiveTimeout: const Duration(seconds: 10),
),
);
}
4.0.0
Details
- Null safety support (Dart >= 2.12).
- The
Interceptor
APIs signature has changed. - Rename
options.merge
tooptions.copyWith
. - Rename
DioErrorType
enums from uppercase to camel style. - Delete
dio.resolve
anddio.reject
APIs (usehandler
instead in interceptors). - Class
BaseOptions
no longer inherits fromOptions
class. - Change
requestStream
type ofHttpClientAdapter.fetch
fromStream<List<int>>
toStream<Uint8List>
. - Download API: Add real uri and redirect information to headers.
Libraries
- dio Migration Guide Plugins
- A powerful HTTP client for Dart and Flutter, which supports global settings, Interceptors, FormData, aborting and canceling a request, files uploading and downloading, requests timeout, custom adapters, etc.